<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page   import="java.util.List"%>
<%@ page   import="grupobbva.pe.operhc.domain.SegUsuario"%>
<%@ page   import="grupobbva.pe.operhc.domain.SegParametros"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel="stylesheet" type="text/css" media="screen" href="<%=request.getContextPath()%>/css/ui.jqgrid.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="<%=request.getContextPath()%>/css/redmond/jquery-ui-1.8.2.custom.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="<%=request.getContextPath()%>/css/operhc_style.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="<%=request.getContextPath()%>/css/buttonOHC.css" />

	<script src="<%=request.getContextPath()%>/js/util/gridUtil.js" type="text/javascript"></script>
	<script src="<%=request.getContextPath()%>/js/jquery-1.7.1.js" type="text/javascript"></script>	
	<script src="<%=request.getContextPath()%>/js/i18n/grid.locale-es.js" type="text/javascript"></script>
	<script src="<%=request.getContextPath()%>/js/jquery.jqGrid.src.js" type="text/javascript"></script>
	<script src="<%=request.getContextPath()%>/js/grid.subgrid.js" type="text/javascript"></script>
	<script src="<%=request.getContextPath()%>/js/util/encabezado.js" type="text/javascript"></script>

<title>Formulario de Usuario</title>

</head>

<%
SegUsuario usuarioSession = (SegUsuario)request.getSession().getAttribute("usuarioSession");
if(!"1".equals(usuarioSession.getAdmin())){ 
response.sendRedirect("operativizacion.jsp");
}
%>

<body>

<html:form method="post" action="mantenimientoUsuario.do?method=guardar" styleId="formUsuario">
<html:hidden property="stNew" styleId="stNew"/>

	<div style="background-color: #0066bb;">
		<font face="Arial Narrow" size=3 color="#FFFFFF"><b>&nbsp;<bean:message key="etiqueta.titleManteUsuario"/></b></font>
	</div>
	
	<br/>

	<table align="center">
		
		<tr>
		<td align="right" valign="middle"> 
            Tipo de Usuario :
        </td>
        <td align="left" valign="middle">
            <html:select name="UsuarioForm" property="tipoUsu" styleId="tipoUsu" style="width: 160px">
                <logic:notEmpty name="UsuarioForm" property="listTipoUsuarios">
                    <html:optionsCollection name="UsuarioForm" property="listTipoUsuarios" value="codigo" label="descripcion"/>
                </logic:notEmpty>
            </html:select>
		</tr>
	
		<tr>
			<td align="right" valign="middle">
				Codigo de Usuario :
			</td>
			<td align="left" valign="middle">
				<%
					String isNewUser = String.valueOf(request.getAttribute("isNewUser"));
						if("true".equalsIgnoreCase(isNewUser)){
				%>
					<html:text property="codigo" styleId="codigo" style="width: 150px" maxlength="50"/>
				<%
					}else{
				%>
					<html:text property="codigo" readonly="true" styleId="codigo" style="width: 150px" maxlength="50"/>
				<%
					}
				%>
				
			</td>
		</tr>
		
		<tr>
		<td align="right" valign="middle"> 
            Es administrador? :
        </td>
        <td align="left" valign="middle">
            <html:checkbox property="stAdmin" styleId="stAdmin" value="S"/>
		</tr>
		
		<tr>
		<td align="right" valign="middle"> 
            Opciones :
        </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
		<tr>
           <td width="47%" class="CellContent" align="right">
           		
           		<html:select property="cmbReasonSource" styleId="cmbReasonSource" multiple="" size="10" style="width: 180px;">
                   
                   <%
                                      	if(request.getAttribute("listOptionsFree")!=null){
                                                         		List listOptionsFree = (List)request.getAttribute("listOptionsFree");
                                                         		for(int i=0; i < listOptionsFree.size(); i++){
                                                         			SegParametros bean = (SegParametros)listOptionsFree.get(i);
                                      %>
                   <html:option value="<%=bean.getCodigo()%>"><%=bean.getDescripcion()%></html:option>
                   <%
                   	}
                                      }
                   %>
                   
               </html:select>            
           </td>
           <td class="CellContent" align="center" width="6%">
               <input type="button" name="btnRight" value="&gt;&gt;" onclick="fxAddSelectedClient(document.getElementById('formUsuario').cmbReasonSource, document.getElementById('formUsuario').cmbReasonDestination)" style="width: 35px;">
               <p />
               <input type="button" name="btnLeft" value="&lt;&lt;" onclick="fxAddSelectedClient(document.getElementById('formUsuario').cmbReasonDestination, document.getElementById('formUsuario').cmbReasonSource)" style="width: 35px;">
           </td>
           <td class="CellContent" width="47%" align="left">
               <html:select property="cmbReasonDestination" styleId="cmbReasonDestination" multiple="" size="10" style="width: 180px;">
               		
					<%
               								if(request.getAttribute("listOptionsUsed")!=null){
               							                   	 	List listOptionsUsed = (List)request.getAttribute("listOptionsUsed");
               							                   	 	for(int i=0; i < listOptionsUsed.size(); i++){
               							                   	 		SegParametros bean = (SegParametros)listOptionsUsed.get(i);
               							%>
                    <html:option value="<%=bean.getCodigo()%>"><%=bean.getDescripcion()%></html:option>
                    <%
                   	 	}
                    }
                    %>
               		
               </html:select>
           </td>
        </tr>
        
        <tr><td>&nbsp;</td></tr>
        <tr><td align="center" valign="middle" colspan="3">
        <%--
        <html:button property="btnGuardar" value="Guardar" onclick="validacionForm('guardar')" />&nbsp;&nbsp;
        <html:button property="btnCancelar" value="Cancelar" onclick="validacionForm('cancelar')" />
         --%>
         
        <a href="#" onclick="validacionForm('guardar');" class="buttonOHC">GUARDAR</a>&nbsp;&nbsp;
        <a href="#" onclick="validacionForm('cancelar');" class="buttonOHC">CANCELAR</a>
        
        </td></tr>
        
	</table>

</html:form>
</body>
<script type="text/javascript" charset="UTF-8">

var rutaContexto1 = location.pathname;
var rutaContexto2 = "<%=request.getContextPath()%>";
var rutaContexto  = rutaContexto1.substr(0, rutaContexto1.indexOf(rutaContexto2)) + rutaContexto2;

function marcarTodos(id){
    var els = document.getElementById(id).options;
    for(var z = 0; z < els.length; z++)
        els[z].selected = true;
}

function validacionForm(operacion){
	
	var formulario = document.getElementById('formUsuario');
	if(operacion == 'guardar'){
		
		var codigoUsuario = formulario.codigo.value;
		if($.trim(codigoUsuario) == ''){
			alert('El codigo de usuario es obligatorio.');
			return false;
		}

		marcarTodos('cmbReasonSource');
		marcarTodos('cmbReasonDestination');
		formulario.action = rutaContexto+'/mantenimientoUsuario.do?method=guardar';
		formulario.submit();
		
	}else{
		
		formulario.action = rutaContexto+'/mantenimientoUsuario.do?method=verListado';
		formulario.submit();
	}
}

function fxAddSelectedClient(fuente, destino) {
    if(fxVerifyMultipledLines(fuente) > 1) {
        var i=0;
        while (i<fuente.options.length) {
            if(fxVerifyMultipledLines(fuente) > 1) {
                if(fuente.options[i].selected == true) {
                    try {
                        var indice1 = i;
                        var indice2 = destino.length;
                        option = new Option(fuente.options[indice1].text, fuente.options[indice1].value);
                        destino.options[indice2] = option;
                        fuente.options[indice1] = null;
                    }
                    catch(exception) {
                        alert(exception.description);
                    }
                }
                else {
                    i++;
                }
            }
            else {
                i=fuente.options.length;
            }
        }
    }
    if(fxVerifyMultipledLines(fuente) <= 1) {
        fxManageSwapSelect(fuente, destino);
    }
}

function fxVerifyMultipledLines(fuente) {
    var cont = 0;
    var a=0;
    for(a=0;a<fuente.options.length;a++) {
        if(fuente.options[a].selected == true) {
            cont++;
        }
    }
    return cont;
}

function fxManageSwapSelect(fuente, destino) {
    try {
        var indice1 = fuente.options.selectedIndex;
        try {
            if(indice1 == -1) {
                fuente.options[0].selected=true;
                indice1 = fuente.options.selectedIndex;
            }
        }
        catch(exception) {
        }
        var indice2 = destino.length;
        option = new Option(fuente.options[indice1].text, fuente.options[indice1].value);
        destino.options[indice2]=option;
        fuente.options[indice1] = null;
        try {
            fuente.options[indice1].selected=true;
        }
        catch(exception) {
            fuente.options[indice1-1].selected=true;
        }
    }
    catch(exception) {
    }
}
</script>
</html>